package com.wechat.page.utils;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import com.swak.Constants;
import com.swak.utils.time.DateTimes;

/**
 * 时间格式化
 * 
 * @author 618lf
 */
public class Times {

	/**
	 * 时间显示的格式化 -- 简短
	 * 
	 * @param lastShowTimeStamp
	 * @return
	 */
	public static String formateLastTimeBrevity(LocalDateTime lastShowTimeStamp) {

		// 当前日期的不对等
		LocalDateTime nowDate = DateTimes.now();
		if (lastShowTimeStamp == null) {
			return DateTimes.getFormatTime(nowDate, Constants.Only_Minute_Format);
		}
		if (nowDate.isBefore(lastShowTimeStamp)) {
			return DateTimes.getFormatTime(lastShowTimeStamp, Constants.Only_Minute_Format);
		}

		// 相差天数
		long offsetDays = DateTimes.betweenTwoTimeSmart(DateTimes.getDayStart(nowDate),
				DateTimes.getDayStart(lastShowTimeStamp), ChronoUnit.DAYS);

		// 同一天
		if (offsetDays == 0) {
			return DateTimes.getFormatTime(lastShowTimeStamp, Constants.Only_Minute_Format);
		}

		// 昨天
		if (offsetDays == 1) {
			return "昨天";
		}

		// 前天
		if (offsetDays == 2) {
			return "前天";
		}

		// 同一周
		if (DateTimes.getWeekOfYear(nowDate) == DateTimes.getWeekOfYear(lastShowTimeStamp)) {
			return DateTimes.getDayName2OfWeek(lastShowTimeStamp);
		}

		// 显示具体的日期
		return DateTimes.getFormatTime(lastShowTimeStamp, Constants.Simple_Day_Format);
	}

	/**
	 * 时间显示的格式化
	 * 
	 * @param lastShowTimeStamp
	 * @return
	 */
	public static String formateLastTime(LocalDateTime lastShowTimeStamp) {

		// 当前日期的不对等
		LocalDateTime nowDate = DateTimes.now();
		if (lastShowTimeStamp == null) {
			return DateTimes.getFormatTime(nowDate, Constants.Only_Minute_Format);
		}
		if (nowDate.isBefore(lastShowTimeStamp)) {
			return DateTimes.getFormatTime(lastShowTimeStamp, Constants.Second_Format);
		}

		// 相差天数
		long offsetDays = DateTimes.betweenTwoTimeSmart(DateTimes.getDayStart(nowDate),
				DateTimes.getDayStart(lastShowTimeStamp), ChronoUnit.DAYS);

		// 同一天
		if (offsetDays == 0) {
			return DateTimes.getFormatTime(lastShowTimeStamp, Constants.Only_Minute_Format);
		}

		// 昨天
		if (offsetDays == 1) {
			return "昨天 " + DateTimes.getFormatTime(lastShowTimeStamp, Constants.Only_Minute_Format);
		}

		// 前天
		if (offsetDays == 2) {
			return "前天 " + DateTimes.getFormatTime(lastShowTimeStamp, Constants.Only_Minute_Format);
		}

		// 同一周
		if (DateTimes.getWeekOfYear(nowDate) == DateTimes.getWeekOfYear(lastShowTimeStamp)) {
			return DateTimes.getDayName2OfWeek(lastShowTimeStamp) + " "
					+ DateTimes.getFormatTime(lastShowTimeStamp, Constants.Only_Minute_Format);
		}

		// 显示具体的日期
		return DateTimes.getFormatTime(lastShowTimeStamp, Constants.Second_Format);
	}

}